package com.spsoft.dmzeinvoice.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.model.Result;
import com.spsoft.core.model.leqidmz.param.*;
import com.spsoft.dmzeinvoice.common.annotation.DmzLog;
import com.spsoft.dmzeinvoice.service.VmsInvoiceService;
import com.spsoft.core.model.leqidmz.response.BaseResponseVO;
import com.spsoft.core.model.leqidmz.response.QueryUploadInvoiceStatusVO;
import com.spsoft.core.model.leqidmz.response.QueryUploadInvoiceSummaryVO;
import com.spsoft.core.model.leqidmz.response.UploadInvoiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"数电票开票相关模块"})
@RequestMapping("/invoice")
@RestController
@RequiredArgsConstructor
public class VmsInvoiceController {

    private final VmsInvoiceService vmsInvoiceService;

    @ApiOperation("数电票上传")
    @AnonymousAccess(value = "/uploadInvoice", method = RequestMethod.POST, permissionCode = "")
    @DmzLog(leqiName = "数电票上传")
    public Result<UploadInvoiceVO> uploadInvoice(@RequestBody @Validated UploadInvoiceParamDto uploadDto) {
        List<UploadInvoiceParams> paramsList = JSONObject.parseArray(uploadDto.getUploadInvoiceJson(), UploadInvoiceParams.class);
        if (CollectionUtil.isEmpty(paramsList) || paramsList.size() > 100) {
            return Result.ValidateError("上传票数不能为空，且最大票量不能超过100");
        }
        return vmsInvoiceService.uploadInvoice(uploadDto) ;
    }

    @ApiOperation("查询数电票上传结果")
    @AnonymousAccess(value = "/queryUploadInvoiceStatus", method = RequestMethod.POST, permissionCode = "")
    @DmzLog(leqiName = "查询数电票上传结果")
    public Result<QueryUploadInvoiceStatusVO> queryUploadInvoiceStatus(@RequestBody @Validated QueryUploadInvoiceStatusParam param) {
        return vmsInvoiceService.queryUploadInvoiceStatus(param);
    }

    @ApiOperation("上传发票汇总确认信息")
    @AnonymousAccess(value = "/uploadInvoiceSummary", method = RequestMethod.POST, permissionCode = "")
    @DmzLog(leqiName = "上传发票汇总确认信息")
    public Result<BaseResponseVO> uploadInvoiceSummary(@RequestBody @Validated UploadInvoiceSummaryParam param) {
        return vmsInvoiceService.uploadInvoiceSummary(param);
    }

    @ApiOperation("查询发票汇总确认信息")
    @AnonymousAccess(value = "/queryUploadInvoiceSummary", method = RequestMethod.POST, permissionCode = "")
    @DmzLog(leqiName = "查询发票汇总确认信息")
    public Result<QueryUploadInvoiceSummaryVO> queryUploadInvoiceSummary(@RequestBody @Validated QueryUploadInvoiceSummaryParam param) {
        return vmsInvoiceService.queryUploadInvoiceSummary(param);
    }

}
